#! /bin/sh
#Demarrage du serveur TinyERP installe dans /usr/local/tiny/server/bin
# dont l'adresse est 192.168.0.3
#La base de donnee automatiquement utilisee est terp
#Un fichier log est cree: /home/terp/tiny_log.txt --quiet --background
# Defaults
RUN_MODE="daemons"

# Variables du script pour TinyERP
TINYPATH=/home/openerp/4.2/server/bin/tinyerp-server.py
TINYPID=/var/run/openerp.pid
TINYLOG=/var/log/openerp.log
TINYDB=gcerp
TINYDBUSER=root
TINYIP=localhost

if [ $1 ]; then
	echo $1'ing server'
else
	set start
fi

case "$1" in
start)
echo " ---`date +\"%D-%H:%M:%S\"`--- Starting TinyERP Server daemon on $TINYDB..." >> $TINYLOG
echo "Starting TinyERP Server daemon on $TINYDB..."
if ! start-stop-daemon --start --chuid $TINYDBUSER --quiet --background --pidfile $TINYPID --make-pidfile --exec $TINYPATH -- --database=$TINYDB --interface=$TINYIP; then
echo " ---`date +\"%D-%H:%M:%S\"`--- ERROR Starting TinyERP Server daemons on $TINYDB..." >> $TINYLOG
exit 1
fi
echo " ---`date +\"%D-%H:%M:%S\"`--- TinyERP Server daemon is started on $TINYDB..." >> $TINYLOG

;;
stop)
echo " ---`date +\"%D-%H:%M:%S\"`--- Stopping TinyERP Server daemon on $TINYDB..." >> $TINYLOG

start-stop-daemon --stop --quiet --pidfile $TINYPID
# Wait a little and remove stale PID file
sleep 1
if [ -f $TINYPID ] && ! ps h `cat $TINYPID` > /dev/null
then
# Stale PID file (nmbd was succesfully stopped),
# remove it (should be removed by nmbd itself IMHO.)
rm -f $TINYPID
fi

echo " ---`date +\"%D-%H:%M:%S\"`--- TinyERP Server daemon stopped on $TINYDB..." >> $TINYLOG
echo "TinyERP Server daemon stopped on $TINYDB..."
;;
restart|force-reload)
$0 stop
sleep 1
$0 start
;;
*)
echo " ---`date +\"%D-%H:%M:%S\"`--- Usage: /etc/init.d/tiny {start|stop|reload|restart|force-reload}" >> $TINYLOG
exit 1 
;;
esac

exit 0
